/*
 * @Author: your name
 * @Date: 2020-04-09 15:32:18
 * @LastEditTime: 2020-11-04 10:58:18
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \cell-project\src\common\utils\axiosApi.js
 */
import axios from 'axios'
import { getStorage } from '@/common/utils/auth'
import ErrorMessage from '@/common/utils/errorMessage'
import { MessageBox } from 'element-ui'

const service = axios.create({
  baseURL: './',
  timeout: 30000,
  withCredentials: true,
  credentials: 'include'
})
service.interceptors.request.use(
  config => {
    // token
    if (getStorage()) {
      config.headers['tg-token'] = getStorage()
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    // 走本地JSON文件场景
    if (response.config.url.includes('.json')) {
      return Promise.resolve(response)
    }
    if (~~response.data.code === 204) {
      // 无数据
      return Promise.resolve(response)
    } else {
      if (~~response.data.code !== 200) {
        return Promise.reject(response)
      } else {
        return Promise.resolve(response)
      }
    }
  },
  error => {
    if (!error.response) {
      // 服务器请求失败时错误提示
      MessageBox({
        message: `请求超时${ErrorMessage.API_ERROR_LOAD}`,
        showCancelButton: false,
        confirmButtonText: '确定',
        type: 'error',
        callback () {}
      })
    } else {
      // 走本地JSON文件场景
      if (error.response.config.url.includes('.json')) {
        return Promise.resolve(error.response)
      }
      switch (error.response.status) {
        case 400:
          error.message = ErrorMessage.STATUS_400
          break
        case 401:
          MessageBox({
            message: '失效，请再次登录！',
            showCancelButton: false,
            confirmButtonText: '确定',
            type: 'error',
            callback () {}
          })
          break
        case 403:
          error.message = ErrorMessage.STATUS_403
          break
        case 404:
          error.message = ErrorMessage.STATUS_404
          break
        case 408:
          error.message = ErrorMessage.STATUS_408
          break
        case 500:
          error.message = ErrorMessage.STATUS_500
          break
        case 501:
          error.message = ErrorMessage.STATUS_501
          break
        case 502:
          error.message = ErrorMessage.STATUS_502
          break
        case 503:
          error.message = ErrorMessage.STATUS_503
          break
        case 504:
          error.message = ErrorMessage.STATUS_504
          break
        case 505:
          error.message = ErrorMessage.STATUS_505
          break
        default:
      }
      MessageBox({
        message: `请求失败-${error.message}`,
        showCancelButton: false,
        confirmButtonText: '确定',
        type: 'error',
        callback () {}
      })
    }
    return Promise.reject(error)
  }
)

export default function (url, method, model, params, isPost) {
  const _config = {
    url: url,
    method: method
  }
  const _params = params
    ? model
      ? model.request(JSON.parse(JSON.stringify(params)))
      : params
    : ''
  if (method.toLowerCase() === 'post' || isPost) {
    _config.data = _params
  } else {
    _config.params = _params
  }
  return new Promise((resolve, reject) => {
    return service(_config)
      .then(response => {
        resolve(model ? model.response(response.data) : response.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}
